/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File HashedNodeset.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.activeData;

import il.ac.biu.cs.grossmm.api.data.UpdateType;

import java.util.HashSet;
import java.util.Set;

public class HashedNodeset {
	/**
	 * A 2x4 array, where element [i][j] is a set of nodes with
	 * <ul>
	 * <li> i==0 : current state
	 * <li> u==1 : new state, being created
	 * <li> update type with ordinal() == j </ul
	 */
	Set<HashedNode>[][] nodes;

	final HashedNode parentNode;

	final HashedRootNode<?> rootNode;

	final int parentIndex;

	@SuppressWarnings("unchecked")
	HashedNodeset(HashedNode parentNode, int parentIndex) {
		this.parentNode = parentNode;
		this.parentIndex = parentIndex;
		this.rootNode = (HashedRootNode<?>) parentNode.getRoot();
		nodes = new HashSet[2][];
		for (int i = 0; i < 2; i++) {
			nodes[i] = new HashSet[4];
			for (int j = 0; j < 4; j++) {
				nodes[i][j] = new HashSet(3);
			}
		}
	}

	void add(HashedNode node) {
		nodes[1][UpdateType.ADDED.ordinal()].add(node);
		node.updateType[1] = UpdateType.ADDED;

		parentNode.changed();
	}

	synchronized void moveTo(HashedNode node, UpdateType u) {
		assert node.updateType[1] != u;
		assert node.updateType[1] != UpdateType.REMOVED;

		nodes[1][node.updateType[1].ordinal()].remove(node);
		nodes[1][u.ordinal()].add(node);

		parentNode.changed();
	}

	public void shift() {
		for (HashedNode n : nodes[1][UpdateType.UNCHANGED.ordinal()])
			if (n.updateType[0] != UpdateType.UNCHANGED)
				n.shift2();

		for (HashedNode n : nodes[1][UpdateType.CHANGED.ordinal()])
			n.shift();

		for (HashedNode n : nodes[1][UpdateType.ADDED.ordinal()])
			n.shift();

		for (HashedNode n : nodes[1][UpdateType.REMOVED.ordinal()])
			n.shift();

		Set<HashedNode>[] tmp = nodes[0];
		nodes[0] = nodes[1];

		if (parentNode.updateType[1] != UpdateType.REMOVED) {
			tmp[0].clear();
			tmp[0].addAll(nodes[0][0]);
			tmp[0].addAll(nodes[0][1]);
			tmp[0].addAll(nodes[0][2]);
			tmp[0].removeAll(nodes[0][3]);

			// tmp[0].addAll(tmp[1]);
			// tmp[0].addAll(tmp[2]);
			// tmp[0].addAll(nodes[0][2]);
			// tmp[0].removeAll(nodes[0][3]);
			tmp[1].clear();
			tmp[2].clear();
			tmp[3].clear();
			nodes[1] = tmp;
		}
	}

	public void clear() {
		// TODO Implement clear
		// 
		// throw new RuntimeException("Not implemented");
	}

	/**
	 * Shift epochs for all subnodes of a node which changed between the old and
	 * the current epochs (0) and did not change between the current and the new
	 * epochs (1).
	 * 
	 */
	public void shift2() {

		// all subnodes of this node did not change in (1)
		// shift-2 all nodes changed in (0)
		for (HashedNode n : nodes[0][UpdateType.UNCHANGED.ordinal()])
			n.shift2();
	}
}
